﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using PhotoGallery.DAL;
using PhotoGallery.Model;
namespace PhotoGallery.DLL
{
    public class PictureDataAccess
    {
        public pictureDataContext pdc = new pictureDataContext();
         
          /*插入新纪录*/
        public void insert(String uid, String title,String desp, String aid,String pic) 
        {
            picture picture = new picture();
            picture.uid = int.Parse(uid);
            picture.title = title;
            picture.desp = desp;
            picture.aid = int.Parse(aid);
            picture.picture1 = pic;
            picture.rank = 0;
            picture.share = 0;
            picture.time = DateTime.Now.ToShortTimeString();
            picture.state ='0';
            pdc.pictures.InsertOnSubmit(picture);
            pdc.SubmitChanges();
        }
        /*上传的是封面*/
        public void insert(string uid, string title, string desp,string aid, string pic, string state)
        {
            picture picture = new picture();
            picture.uid = int.Parse(uid);
            picture.title = title;
            picture.aid = int.Parse(aid);
            picture.picture1 = pic;
            picture.rank = 0;
            picture.share = 0;
            picture.time = DateTime.Now.ToShortTimeString();
            picture.state = '1';
            pdc.pictures.InsertOnSubmit(picture);
            pdc.SubmitChanges();
        }
        /*删除记录*/
        public void delete(int id) 
        {
            var result = from m in pdc.pictures
                         where m.id == id
                         select m;
            foreach (picture m in result)
                pdc.pictures.DeleteOnSubmit(m);
            pdc.SubmitChanges();
        }

        /*列出全部图片*/
        public List<Picture> getAllthePictures()
        {
            List<Picture> all = new List<Picture>();
            var query = from m in pdc.pictures
                        select m;
            foreach (picture m in query)
            {
                all.Add(new Picture(m.id.ToString(), m.uid.ToString(), m.title,m.desp, m.aid.ToString(), m.picture1, m.rank.ToString(), m.share.ToString(), m.time, m.state.ToString()));
            }
            return all;
        }

        /*按照图片id查找图片*/
        public Picture searchpictureById(string id)
        {   int pid=int.Parse(id);
        var query = pdc.pictures.Where(m => m.id == pid).Select(m => new Picture(m.id.ToString(), m.uid.ToString(), m.title, m.desp, m.aid.ToString(), m.picture1, m.rank.ToString(), m.share.ToString(), m.time, m.state.ToString()));
            foreach (var m in query)
                if (m != null)
                    return m;
                else
                    return null;
            return null;
        }
        /*按照相册id搜索图片*/
        public List<Picture> searchpictureByAlbumname(string aid) 
        {
            List<Picture> list = new List<Picture>();
            int id = int.Parse(aid);
            var query = pdc.pictures.Where(m => m.aid == id).Select(m => new Picture(m.id.ToString(), m.uid.ToString(), m.title, m.desp, m.aid.ToString(), m.picture1, m.rank.ToString(), m.share.ToString(), m.time, m.state.ToString()));
            foreach (var m in query)
                if (m != null)
                   list.Add(m);
            return list;
        }
        /*找到某相册的封面图片(state字段为1的)*/
        public Picture getCoverPicbyAlbumId(string aid) 
        {
            var query = pdc.pictures.Where(m => m.aid == int.Parse(aid) && m.state == '1').Select(m => new Picture(m.id.ToString(), m.uid.ToString(), m.title, m.desp, m.aid.ToString(), m.picture1, m.rank.ToString(), m.share.ToString(), m.time, m.state.ToString()));
            foreach (var m in query)
                if (m != null)
                    return m;
                else
                    return null;
            return null;
        }
        /*按照用户uid搜索图片*/
        public List<Picture> searchpictureByUsername(string uid) 
        {
            List<Picture> list = new List<Picture>();
            var query = pdc.pictures.Where(m => m.uid == int.Parse(uid)).Select(m => new Picture(m.id.ToString(), m.uid.ToString(), m.title, m.desp, m.aid.ToString(), m.picture1, m.rank.ToString(), m.share.ToString(), m.time, m.state.ToString()));
            foreach (var m in query)
                if (m != null)
                    list.Add(m);
            return list;
        }
        /*按照用户uid和相册aid搜索图片*/
        public Picture searchPicbyUidndAid(String uid, String aid) 
        {
            var query = pdc.pictures.Where(m => m.uid == int.Parse(uid) && m.aid == int.Parse(aid)).Select(m => new Picture(m.id.ToString(), m.uid.ToString(), m.title, m.desp, m.aid.ToString(), m.picture1, m.rank.ToString(), m.share.ToString(), m.time, m.state.ToString()));
            foreach (var m in query)
                if (m != null)
                    return m;
                else
                    return null;
            return null;
        }
        /*按照用户名和文件名搜索图片*/
        public Picture searchPicbyUidndFileName(String uid, String filename) 
        {
            var query = pdc.pictures.Where(m => m.uid == int.Parse(uid) && m.title == filename).Select(m => new Picture(m.id.ToString(), m.uid.ToString(), m.title, m.desp, m.aid.ToString(), m.picture1, m.rank.ToString(), m.share.ToString(), m.time, m.state.ToString()));
            foreach (var m in query)
                if (m != null)
                    return m;
                else
                    return null;
            return null;
        }
        /*修改图片信息*/
        public void modifypicture(string id,Picture mypicture) 
        {
            var result = from m in pdc.pictures
                         where m.id == int.Parse(id)
                         select m;
            foreach (picture u in result) 
            {
                u.title = mypicture.Title;
                u.desp = mypicture.Desp;
                u.aid = int.Parse(mypicture.Aid);
                u.state = char.Parse(mypicture.State);
            }
            pdc.SubmitChanges();
        }
        /*设为封面*/
        public void setCover(string pid,string aid)
        {
            var result1 = from m in pdc.pictures
                         where m.id == int.Parse(pid) && m.aid==int.Parse(aid)
                         select m;//准备设为新封面的图片
            var result2 = from n in pdc.pictures
                          where n.aid == int.Parse(aid) && n.state == '1'
                          select n;//之前的封面图片
            foreach (picture u in result2) 
            {
                u.state = '0';//取消封面图
            }
            foreach (picture v in result1) 
            {
                v.state = '1';//设置新封面图
            }
            pdc.SubmitChanges();
        }

        
    }
}
